import os
import platform
import shutil
import sys
import requests



class TVRename:
    def __init__(self, dir_path):
        self.dir_path = dir_path



    def rename_file(self, old_name,new_name):
        paths = os.walk(self.dir_path)
        for path, dir_list, file_list in os.walk(self.dir_path):
            for file_name in file_list:
                # print(file)
                if (old_name in file_name) and ('.xltd' not in file_name) and (not os.path.exists(file_name + '.aria2')) and ('.aria2' not in file_name):
                    f_new = file_name.replace(f'{old_name}',f'{new_name}')
                    print(file_name)
                    print(f_new)
                    old_path = os.path.join(path, file_name)
                    print(old_path)
                    new_path = os.path.join(path, f_new)

                    try:
                        if os.path.exists(old_path):


                            print("oldname: " + old_path)
                            print("newname:" + new_path)
                            print("-----------------------")
                            os.rename(old_path, new_path)
                    except Exception as e:
                        print(e)
                        print("have some error,skip......")




dir_list = sys.argv
for video_dir in dir_list:
    print("rename path: " + video_dir)
    rename_list = requests.get(url='https://gitee.com/team_space/mynet_info/raw/master/emby_xunlei_rename_list.txt').text

    for line in rename_list.split('\n'):
        # print(line)
        if len(line.split('==')) == 2:
            old_name = str(str(line).split('==')[0])
            new_name = str(str(line).split('==')[1])

            process = TVRename(video_dir)
            process.rename_file(old_name, new_name)
    print("rename finished.")


## cd /tmp/;
## rm -f emby_xunlei_rename.py emby_xunlei_rename_list.txt;
## wget https://gitee.com/team_space/mynet_info/raw/master/emby_xunlei_rename.py;
## wget https://gitee.com/team_space/mynet_info/raw/master/emby_xunlei_rename_list.txt;
## python3 emby_xunlei_rename.py /volume4/迅雷/下载 /volume4/video/xunlei
